#include <gtk/gtk.h>
#include <glade/glade.h>
#include <jni.h>
#include <string.h>
#include <stdlib.h>
#include "gtkwire_command.h"
#include "gtkwire_app.h"
#include "gtkwire_utils.h"
#include "gtkwire_databank.h"

/*Spacing types*/
#define ROW_SPACING 		0
#define COLUMN_SPACING 		1
#define ALL_ROWS_SPACING  	2
#define ALL_COLUMNS_SPACING 	3

/* Attach type*/
#define ATTACH_FULL		0
#define ATTACH_DEFAULT		1

/* Status bar context id */
#define CONTEXT_ID 		1

/* Command definitions */
//##defines##
#define ADD_PANED 		"add_paned"
#define ADD_WIDGET 		"add_widget"
#define ADD_WITH_VIEWPORT 	"add_with_viewport"
#define APPEND 			"append"
#define ATTACH 			"attach"
#define DESTROY_WIDGET		"destroy_widget"
#define DEICONIFY		"deiconify"
#define EXTRACT_LABEL "extract_label"
#define GET_ACTIVE 		"get_active"
#define GET_CONTEXT_ID 		"get_context_id"
#define GET_DRAW_VALUE		"get_draw_value"
#define GET_FILENAME 		"get_filename"
#define GET_INDEX 		"get_index"
#define GET_SELECTED_COLOR	"get_selected_color"
#define GET_SELECTED_FONT 	"get_selected_font"
#define GET_SIZE 		"get_size"
#define GET_TEXT		"get_text"
#define GET_TOGGLED 		"get_toggled"
#define GET_VALUE		"get_value"
#define GRAB_FOCUS		"grab_focus"
#define HAS_FOCUS		"has_focus"
#define HIDE 			"hide"
#define ICONIFY			"iconify"
#define INSERT 			"insert"
#define IS_VISIBLE		"is_visible"
#define MAXIMIZE		"maximize"
#define PACK_END 		"pack_end"
#define PACK_PANED 		"pack_paned"
#define PACK_START 		"pack_start"
#define PREPEND 		"prepend"
#define PULSE 			"pulse"
#define REMOVE_ITEM 		"remove_item"
#define REMOVE_WIDGET 		"remove_widget"
#define REORDER 		"reorder"
#define RESIZE 			"resize"
#define SET_ACTIVE 		"set_active"
#define SET_ACTIVE_INDEX	"set_active_index"
#define SET_ADJUSTMENT		"set_adjustement"
#define SET_ADJUSTMENT_VALUES "set_adjustment_values"
#define SET_ALIGNMENT 		"set_alignment"
#define SET_ANGLE 		"set_angle"
#define SET_ASPECT_RATIO 	"set_aspect_ratio"
#define SET_BORDER 		"set_border"
#define SET_CHILD 		"set_child"
#define SET_COLOR 		"set_color"
#define SET_CURRENT_FOLDER 	"set_current_folder"
#define SET_CURRENT_PAGE 	"set_current_page"
#define SET_DISABLED 		"set_disabled"
#define SET_DRAW 		"set_draw"
#define SET_DIGITS 		"set_digits"
#define SET_EDITABLE 		"set_editable"
#define SET_ELLIPSIZE 		"set_ellipsize"
#define SET_EXPAND 		"set_expand"
#define SET_FILENAME 		"set_filename"
#define SET_FOCUS_ON_CLICK 	"set_focus_on_click"
#define SET_FONT 		"set_font"
#define SET_FROM_STOCK 		"set_from_stock"
#define SET_HOMOGENOUS 		"set_homogenous"
#define SET_IMAGE 		"set_image"
#define SET_INCREMENTS 		"set_increments"
#define SET_INTEND 		"set_intend"
#define SET_ITEMS		"set_items"
#define SET_JUSTIFICATION 	"set_justification"
#define SET_LABEL 		"set_label"
#define SET_LAYOUT 		"set_layout"
#define SET_MARKUP "set_markup"
#define SET_MAX 		"set_max"
#define SET_MODE 		"set_mode"
#define SET_NUMERIC		"set_numeric"
#define SET_ORIENTATION 	"set_orientation"
#define SET_PADDING 		"set_padding"
#define SET_POSITION 		"set_position"
#define SET_RANGE		"set_range"
#define SET_RESIZABLE		"set_resizable"
#define SET_SENSITIVE		"set_sensitive"
#define SET_SHADOW 		"set_shadow"
#define SET_SHOW 		"set_show"
#define SET_SIZE 		"set_size"
#define SET_SPACING 		"set_spacing"
#define SET_STEP 		"set_step"
#define SET_STOCK 		"set_stock"
#define SET_STYLE 		"set_style"
#define SET_TEXT 		"set_text"
#define SET_TITLE		"set_title"
#define SET_TOOLTIP		"set_tooltip"
#define SET_UPDATE_POLICY	"set_update_policy"
#define SET_USE_SIZE 		"set_use_size"
#define SET_USE_UNDERLINE 	"set_use_underline"
#define SET_VALUE		"set_value"
#define SET_WIDTH 		"set_width"
#define SET_WRAP 		"set_wrap"
#define SHOW 			"show"
#define SHOW_ALL 		"show_all"
#define SHOW_SIZE 		"show_size"
#define SIZE_REQUEST 		"size_request"
#define STACK_OP 		"stack_op"
#define STACK_OP_CR 		"stack_op_cr"
#define TW_APPEND 		"tw_append"
#define TW_COL_SET_SORT_INDICATOR 	"tw_col_set_sort_indicator"
#define TW_GET_SELECTED 	"tw_get_selected"
#define TW_LS_APPEND 		"tw_ls_append"
#define TW_LS_SET_VALUE 	"tw_ls_set_value"
#define TW_SET_CURSOR 		"tw_set_cursor"
#define TW_SET_MODEL 		"tw_set_model"
#define TW_SET_SELECTION 	"tw_set_selection"
#define TW_SET_SEL_ALL 		"tw_set_sel_all"
#define TW_SET_SEL_ROW 		"tw_set_sel_row"
#define TW_TM_GET_FIRST 	"tw_tm_get_first"
#define TW_TM_NEXT 		"tw_tm_next"
#define UNMAXIMIZE		"unmaximize"

/* Command handler funcs */
//##funcdecs##
static void add_paned 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void add_widget 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void add_with_viewport 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void append 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void attach 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void destroy_widget 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void deiconify			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void extract_label (GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_active 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_context_id 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_draw_value		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_filename 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_index 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_selected_color		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_selected_font		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_size 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_text			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_toggled 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void get_value			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void grab_focus			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void has_focus			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void hide			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void iconify			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void insert 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void is_visible			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void maximize			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void pack_end 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void pack_paned 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void pack_start 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void prepend 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void pulse 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void remove_item 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void remove_widget 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void reorder 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void resize 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_active 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_active_index		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_adjustment 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_adjustment_values 	(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_alignment 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_angle 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_aspect_ratio 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_border 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_child 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_color 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_current_folder 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_current_page 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_disabled 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_draw 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_digits 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_editable 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_ellipsize 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_expand 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_filename 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_font 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_focus_on_click		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_from_stock 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_homogenous 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_image 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_increments 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_intend 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_items			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_justification 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_label			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_layout 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_markup (GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_max 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_mode 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_numeric			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_orientation 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_padding 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_position 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_range			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_resizable		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_sensitive		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_shadow 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_show 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_size			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_spacing 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_step 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_stock 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_style 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_text			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_title			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_tooltip			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_use_size 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_update_policy		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_use_underline 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_value			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_width 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void set_wrap 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void show			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void show_all 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void show_size 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void size_request 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void stack_op 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void stack_op_cr 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_append 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_col_set_sort_indicator 	(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_get_selected 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_ls_append 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_ls_set_value 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_set_cursor 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_set_model 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_set_sel_all 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_set_sel_row 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_set_selection 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_tm_get_first 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void tw_tm_next 			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);
static void unmaximize			(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);


/* Command handler function prototype */
typedef void   (*CommandFunc) 		(GtkWidget *widget, gint data_type, gpointer data, gpointer data2);

/* Prints info on widget that was not found */
static void wrong_widget		(const gchar* widget_name, const gchar* command, gpointer command_value_obj);

/* Hastable for command handler functions */
static GHashTable *command_func_table = NULL;

/***************************************************************************/
/*** COMMAND CONNECTOR FUNCTIONS                                         ***/
/***************************************************************************/
void
init_comm ()
{
/* Builds command handler function table */
//##hashlines##
	command_func_table = g_hash_table_new (g_str_hash, g_str_equal);
	g_hash_table_insert (command_func_table, ADD_PANED, add_paned);
	g_hash_table_insert (command_func_table, ADD_WIDGET, add_widget);
	g_hash_table_insert (command_func_table, ADD_WITH_VIEWPORT, add_with_viewport);
	g_hash_table_insert (command_func_table, APPEND, append);
	g_hash_table_insert (command_func_table, ATTACH, attach);
	g_hash_table_insert (command_func_table, DEICONIFY, deiconify);
	g_hash_table_insert (command_func_table, DESTROY_WIDGET, destroy_widget);
	g_hash_table_insert (command_func_table, EXTRACT_LABEL, extract_label);
	g_hash_table_insert (command_func_table, GET_ACTIVE, get_active);
	g_hash_table_insert (command_func_table, GET_CONTEXT_ID, get_context_id);
	g_hash_table_insert (command_func_table, GET_DRAW_VALUE, get_draw_value);
	g_hash_table_insert (command_func_table, GET_FILENAME, get_filename);
	g_hash_table_insert (command_func_table, GET_INDEX, get_index);
	g_hash_table_insert (command_func_table, GET_SELECTED_COLOR, get_selected_color);
	g_hash_table_insert (command_func_table, GET_SELECTED_FONT, get_selected_font);
	g_hash_table_insert (command_func_table, GET_SIZE, get_size);
	g_hash_table_insert (command_func_table, GET_TEXT, get_text);
	g_hash_table_insert (command_func_table, GET_TOGGLED, get_toggled);
	g_hash_table_insert (command_func_table, GET_VALUE, get_value);
	g_hash_table_insert (command_func_table, GRAB_FOCUS, grab_focus);
	g_hash_table_insert (command_func_table, HAS_FOCUS, has_focus);
	g_hash_table_insert (command_func_table, HIDE, hide);
	g_hash_table_insert (command_func_table, ICONIFY, iconify);
	g_hash_table_insert (command_func_table, INSERT, insert);
	g_hash_table_insert (command_func_table, IS_VISIBLE, is_visible);
	g_hash_table_insert (command_func_table, MAXIMIZE, maximize);
	g_hash_table_insert (command_func_table, PACK_END, pack_end);
	g_hash_table_insert (command_func_table, PACK_PANED, pack_paned);
	g_hash_table_insert (command_func_table, PACK_START, pack_start);
	g_hash_table_insert (command_func_table, PREPEND, prepend);
	g_hash_table_insert (command_func_table, PULSE, pulse);
	g_hash_table_insert (command_func_table, REMOVE_ITEM, remove_item);
	g_hash_table_insert (command_func_table, REMOVE_WIDGET, remove_widget);
	g_hash_table_insert (command_func_table, REORDER, reorder );
	g_hash_table_insert (command_func_table, RESIZE, resize );
	g_hash_table_insert (command_func_table, SET_ACTIVE, set_active);
	g_hash_table_insert (command_func_table, SET_ACTIVE_INDEX, set_active_index);
	g_hash_table_insert (command_func_table, SET_ADJUSTMENT, set_adjustment);
	g_hash_table_insert (command_func_table, SET_ADJUSTMENT_VALUES, set_adjustment_values);
	g_hash_table_insert (command_func_table, SET_ALIGNMENT, set_alignment );
	g_hash_table_insert (command_func_table, SET_ANGLE, set_angle);
	g_hash_table_insert (command_func_table, SET_ASPECT_RATIO, set_aspect_ratio);
	g_hash_table_insert (command_func_table, SET_BORDER, set_border);
	g_hash_table_insert (command_func_table, SET_CHILD, set_child);
	g_hash_table_insert (command_func_table, SET_COLOR, set_color);
	g_hash_table_insert (command_func_table, SET_CURRENT_FOLDER, set_current_folder);
	g_hash_table_insert (command_func_table, SET_CURRENT_PAGE, set_current_page);
	g_hash_table_insert (command_func_table, SET_DISABLED, set_disabled);
	g_hash_table_insert (command_func_table, SET_DRAW, set_draw);
	g_hash_table_insert (command_func_table, SET_DIGITS, set_digits);
	g_hash_table_insert (command_func_table, SET_EDITABLE, set_editable);
	g_hash_table_insert (command_func_table, SET_ELLIPSIZE, set_ellipsize);
	g_hash_table_insert (command_func_table, SET_EXPAND, set_expand);
	g_hash_table_insert (command_func_table, SET_FILENAME, set_filename);
	g_hash_table_insert (command_func_table, SET_FOCUS_ON_CLICK, set_focus_on_click);
	g_hash_table_insert (command_func_table, SET_FONT, set_font);
	g_hash_table_insert (command_func_table, SET_FROM_STOCK, set_from_stock);
	g_hash_table_insert (command_func_table, SET_HOMOGENOUS, set_homogenous);
	g_hash_table_insert (command_func_table, SET_IMAGE, set_image);
	g_hash_table_insert (command_func_table, SET_INCREMENTS, set_increments);
	g_hash_table_insert (command_func_table, SET_INTEND, set_intend);
	g_hash_table_insert (command_func_table, SET_ITEMS, set_items);
	g_hash_table_insert (command_func_table, SET_JUSTIFICATION, set_justification);
	g_hash_table_insert (command_func_table, SET_LABEL, set_label);
	g_hash_table_insert (command_func_table, SET_LAYOUT, set_layout);
	g_hash_table_insert (command_func_table, SET_MARKUP, set_markup);
	g_hash_table_insert (command_func_table, SET_MAX, set_max);
	g_hash_table_insert (command_func_table, SET_MODE, set_mode);
	g_hash_table_insert (command_func_table, SET_NUMERIC, set_numeric);
	g_hash_table_insert (command_func_table, SET_ORIENTATION, set_orientation);
	g_hash_table_insert (command_func_table, SET_PADDING, set_padding);
	g_hash_table_insert (command_func_table, SET_POSITION, set_position);
	g_hash_table_insert (command_func_table, SET_RANGE, set_range);
	g_hash_table_insert (command_func_table, SET_RESIZABLE, set_resizable);
	g_hash_table_insert (command_func_table, SET_SENSITIVE, set_sensitive);
	g_hash_table_insert (command_func_table, SET_SHADOW, set_shadow);
	g_hash_table_insert (command_func_table, SET_SHOW, set_show);
	g_hash_table_insert (command_func_table, SET_SIZE, set_size);
	g_hash_table_insert (command_func_table, SET_SPACING, set_spacing);
	g_hash_table_insert (command_func_table, SET_STEP, set_step);
	g_hash_table_insert (command_func_table, SET_STOCK, set_stock);
	g_hash_table_insert (command_func_table, SET_STYLE, set_style);
	g_hash_table_insert (command_func_table, SET_TEXT, set_text);
	g_hash_table_insert (command_func_table, SET_TITLE, set_title);
	g_hash_table_insert (command_func_table, SET_TOOLTIP, set_tooltip);
	g_hash_table_insert (command_func_table, SET_USE_SIZE, set_use_size);
	g_hash_table_insert (command_func_table, SET_UPDATE_POLICY, set_update_policy);
	g_hash_table_insert (command_func_table, SET_USE_UNDERLINE, set_use_underline);
	g_hash_table_insert (command_func_table, SET_VALUE, set_value);
	g_hash_table_insert (command_func_table, SET_WIDTH, set_width);
	g_hash_table_insert (command_func_table, SET_WRAP, set_wrap);
	g_hash_table_insert (command_func_table, SHOW, show);
	g_hash_table_insert (command_func_table, SHOW_ALL, show_all);
	g_hash_table_insert (command_func_table, SHOW_SIZE, show_size);
	g_hash_table_insert (command_func_table, SIZE_REQUEST, size_request);
	g_hash_table_insert (command_func_table, STACK_OP, stack_op);
	g_hash_table_insert (command_func_table, STACK_OP_CR, stack_op_cr);
	g_hash_table_insert (command_func_table, TW_APPEND, tw_append);
	g_hash_table_insert (command_func_table, TW_COL_SET_SORT_INDICATOR, tw_col_set_sort_indicator);
	g_hash_table_insert (command_func_table, TW_GET_SELECTED, tw_get_selected);
	g_hash_table_insert (command_func_table, TW_LS_APPEND, tw_ls_append);
	g_hash_table_insert (command_func_table, TW_LS_SET_VALUE, tw_ls_set_value);
	g_hash_table_insert (command_func_table, TW_SET_CURSOR, tw_set_cursor);
	g_hash_table_insert (command_func_table, TW_SET_MODEL, tw_set_model);
	g_hash_table_insert (command_func_table, TW_SET_SELECTION, tw_set_selection);
	g_hash_table_insert (command_func_table, TW_SET_SEL_ALL, tw_set_sel_all);
	g_hash_table_insert (command_func_table, TW_SET_SEL_ROW, tw_set_sel_row);
	g_hash_table_insert (command_func_table, TW_TM_GET_FIRST, tw_tm_get_first );
	g_hash_table_insert (command_func_table, TW_TM_NEXT, tw_tm_next );
	g_hash_table_insert (command_func_table, UNMAXIMIZE, unmaximize);
}

void
do_gtkwire_command (const gchar* widget_name, const gchar* command, gint data_type, gpointer data, gpointer data2)
{
	/* Get widget */
	GtkWidget *widget;

	/* Get command func */
 	CommandFunc command_func;

	/* Calls from GObjects and GtkTreeIter use this path */
	if (strcmp(widget_name, MAGIC_NAME) == 0)
	{
		/* Get command func */
 		command_func = g_hash_table_lookup (command_func_table, command);

		/* Execute command */
		command_func (NULL, data_type, data, data2);
		return;
	}

	widget = get_widget (widget_name);

	/* Handle wrong widget */
	if (widget == NULL)
	{
		wrong_widget (widget_name, command, data);
		return;
	}

	/* Get command func */
 	command_func = g_hash_table_lookup (command_func_table, command);

	/* Execute command */
	command_func (widget, data_type, data, data2);
}

static void  //to do: notify java for error
wrong_widget (const gchar* widget_name, const gchar* command, gpointer command_value_obj)
{
	printf("No widget found for: ");
	printf( widget_name );
	printf( "\n" );
}

/***************************************************************************/
/*** COMMAND HANDLER FUNCS                                               ***/
/***************************************************************************/

//##handlerfuncs##
static void
add_paned (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	const char *a_name = read_str_from_glist (data, 0);
	const char *place = read_str_from_glist (data, 1);
	GtkWidget *add_widget = get_widget (a_name);
	if( strcmp( place, "1" ) == 0 )
		gtk_paned_add1 (GTK_PANED(widget), add_widget);
	else 
		gtk_paned_add2 (GTK_PANED(widget), add_widget);
}

static void
add_widget (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GtkWidget *add_widget = get_widget (data);
	gtk_container_add (GTK_CONTAINER(widget), add_widget);
	gtk_widget_show( add_widget );//should we be doing this?
}

static void
add_with_viewport (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GtkWidget *add_widget = get_widget (data);
	gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(widget), add_widget);
}

static void
append (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	if (GTK_IS_COMBO_BOX(widget))
	{
		const char *option = data;
		gtk_combo_box_append_text (GTK_COMBO_BOX(widget),option);
	}
	else if (GTK_IS_MENU_SHELL(widget))
	{
		GtkWidget *child = get_widget (data);
		gtk_menu_shell_append ( GTK_MENU_SHELL(widget), child );
	}
}

static void
attach (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *msg = data2;
	GList *glist = data;
	GtkWidget *add_widget = get_widget (read_str_from_glist(glist, 0));

	gtk_table_attach (	GTK_TABLE(widget),
				add_widget,
				msg[0],
				msg[1],
				msg[2],
				msg[3],
				msg[4],
				msg[5],
				msg[6],
				msg[7]);
}

static void
deiconify (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gtk_window_deiconify (GTK_WINDOW(widget));
}

static void
destroy_widget (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	 gtk_widget_destroy (widget);
}

static void
extract_label (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GtkWidget *label = gtk_frame_get_label_widget (GTK_FRAME(widget));
	insert_widget (label, data);
}

static void
get_active (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	const char* text = gtk_combo_box_get_active_text (GTK_COMBO_BOX(widget));
	if( text != NULL ) send_glade_signal ( widget, GET_ACTIVE, M_STRING, text );
	else send_glade_signal ( widget, GET_ACTIVE, M_NULL, NULL );
}

static void
get_context_id (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int cid = gtk_statusbar_get_context_id (GTK_STATUSBAR(widget), data);
	send_glade_signal (widget, GET_CONTEXT_ID, M_INT, (gpointer) &cid);
}

static void 
get_draw_value (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean val = gtk_scale_get_draw_value (GTK_SCALE(widget));
	gpointer msg = &val;
	send_glade_signal (widget, GET_DRAW_VALUE, M_BOOLEAN, msg);
}

static void
get_filename (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	char *filename;
	filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(widget));
	send_glade_signal (widget, GET_FILENAME, M_STRING, filename);
	g_free (filename);
}

static void
get_index (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int index = gtk_combo_box_get_active (GTK_COMBO_BOX(widget));
	send_glade_signal (widget, GET_INDEX, M_INT, (gpointer) &index);
}

static void
get_selected_color (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
 	GdkColor color;
	int len = 4;
	int c[len];
	guint16 alpha;

	if (GTK_IS_COLOR_BUTTON(widget)) 
	{
		gtk_color_button_get_color (GTK_COLOR_BUTTON(widget), &color);
	}
	else if (GTK_IS_COLOR_SELECTION_DIALOG(widget)) 
	{
		GtkColorSelectionDialog *sd = GTK_COLOR_SELECTION_DIALOG (widget);
		GtkColorSelection *cs = GTK_COLOR_SELECTION (sd->colorsel);
		gtk_color_selection_get_current_color ( cs, &color );
		alpha = gtk_color_selection_get_current_alpha (cs);
	}

	c[ 0 ] = color.red >> 8;
	c[ 1 ] = color.green >> 8;
	c[ 2 ] = color.blue >> 8;
	c[ 3 ] = alpha >> 8;

	send_glade_signal_two_msg( widget, GET_SELECTED_COLOR, M_INT_ARR, (gpointer) c, &len );
}

static void 
get_selected_font (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	const gchar *font;
	if (GTK_IS_FONT_SELECTION_DIALOG(widget))
	{
		GtkFontSelectionDialog *fsd = GTK_FONT_SELECTION_DIALOG (widget);
		font = gtk_font_selection_dialog_get_font_name (fsd);
	}
	else if (GTK_IS_FONT_BUTTON(widget)) font = gtk_font_button_get_font_name (GTK_FONT_BUTTON(widget));

	send_glade_signal ( widget, GET_SELECTED_FONT, M_STRING, (gpointer) font );
}

static void
get_size (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	
}

static void 
get_text (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	if (GTK_IS_ENTRY(widget))
	{
		const gchar *msg;
		msg = gtk_entry_get_text (GTK_ENTRY(widget));
		send_glade_signal (widget, GET_TEXT, M_STRING, (gpointer) msg);
		//memory leak? , can't kill warnings
	}
	else if (GTK_IS_TEXT_VIEW(widget)) 
	{
		GtkTextIter start, end;
		GtkTextBuffer *buffer;
		gchar *text;
		buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
		gtk_text_buffer_get_bounds (buffer, &start, &end);
		text = gtk_text_iter_get_text (&start, &end);
		send_glade_signal ( widget, GET_TEXT, M_STRING, (gpointer) text );
		g_free (text);
	}
}

static void
get_toggled (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean val = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget));
	gpointer msg = &val;
	send_glade_signal ( widget, GET_TOGGLED, M_BOOLEAN, msg );
}

static void 
get_value (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	if (widget_is(widget, "GtkSpinButton")) {
		GtkAdjustment *adjustment = gtk_spin_button_get_adjustment( GTK_SPIN_BUTTON(widget));
		gdouble val = gtk_adjustment_get_value( adjustment );
		send_glade_signal (widget, GET_VALUE, M_DOUBLE, (gpointer) &val);
		return;
	}
	/*GtkRange and sub classes*/
	else {
		gdouble val = gtk_range_get_value (GTK_RANGE(widget));
		send_glade_signal (widget, GET_VALUE, M_DOUBLE, (gpointer) &val);
	}
}

static void
grab_focus (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	  gtk_widget_grab_focus (widget);
}

static void
has_focus (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean val = GTK_WIDGET_HAS_FOCUS( widget );
	gpointer msg = &val;
	send_glade_signal (widget, HAS_FOCUS, M_BOOLEAN, msg);
}

static void
hide (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gtk_widget_hide (widget);
}

static void
iconify (GtkWidget *widget, gint data_type, gpointer data, gpointer data2  )
{
	gtk_window_iconify (GTK_WINDOW(widget));
}

static void
insert (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GList *optGL = data;
	GList *indexGL = optGL->next;

	const char *txtdata = optGL->data;
	int index = atoi(indexGL->data);

	if (GTK_IS_COMBO_BOX(widget))
		gtk_combo_box_insert_text (GTK_COMBO_BOX(widget), index, txtdata );
	else//GtkToolbar
	{
		GtkWidget *item = get_widget(txtdata);
		gtk_toolbar_insert (GTK_TOOLBAR(widget), GTK_TOOL_ITEM(item), index);
	}
}

static void
is_visible (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	gboolean val = GTK_WIDGET_VISIBLE ( widget );
	gpointer msg = &val;
	send_glade_signal (widget, IS_VISIBLE, M_BOOLEAN, msg);
}
static void
maximize (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	gtk_window_maximize (GTK_WINDOW(widget));
}

static void
pack_end (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
/*
str_to_boolean(
 int i;
  double d;
  long l;

  i = atoi("1");*/
}

static void
pack_paned (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	const char *place = read_str_from_glist (data, 0);
	gboolean resize = str_to_boolean (read_str_from_glist(data, 1));
	gboolean shrink = str_to_boolean (read_str_from_glist(data, 2));
	const char *a_name = read_str_from_glist (data, 3);

	GtkWidget *add_widget = get_widget (a_name);

	if (strcmp( place, "1" ) == 0)
		gtk_paned_pack1 (GTK_PANED(widget), add_widget, resize, shrink);
	else
		gtk_paned_pack2 (GTK_PANED(widget), add_widget, resize, shrink);

}

static void
pack_start (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GList *child = data;
	GList *expand = child->next;
	GList *fill = expand->next;
	GList *padding = fill->next;
	
	const char *ch = child->data;
	GtkWidget *add_widget = get_widget (ch);
	gboolean exp = str_to_boolean (expand->data);
	gboolean fi = str_to_boolean (fill->data);
	int p = atoi (padding->data);

	gtk_box_pack_start (GTK_BOX(widget), add_widget, exp, fi, p);
}

static void
prepend (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	if (GTK_IS_COMBO_BOX(widget)) 
	{
		const char *option = data;
		gtk_combo_box_prepend_text (GTK_COMBO_BOX(widget),option);
	}

	if (GTK_IS_MENU_SHELL(widget)) 
	{
		const char *child_name = data;
		GtkWidget *child = get_widget (child_name);
		gtk_menu_shell_prepend (GTK_MENU_SHELL(widget), child);
	}
}

static void
pulse (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gtk_progress_bar_pulse (GTK_PROGRESS_BAR(widget));
}

static void
remove_item (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *index = data;
	gtk_combo_box_remove_text (GTK_COMBO_BOX(widget), *index);
}

static void
remove_widget (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	const char *w_name = data;
	GtkWidget *remove_widget = get_widget (w_name);
	gtk_container_remove (GTK_CONTAINER(widget), GTK_WIDGET(remove_widget));
}

static void
reorder (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{

}

static void
resize (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *size = data;
	int rows = size[ 0 ];
	int columns = size[ 1 ];
	gtk_table_resize (GTK_TABLE(widget), (guint) rows, (guint) columns);
}

static void
set_active (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(widget), *b);
}

static void
set_active_index (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	int *index = data;
	gtk_combo_box_set_active ( GTK_COMBO_BOX(widget), *index );
}

static void
set_adjustment (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *gid = data;
	GtkAdjustment *adjustment = NULL;
	adjustment = GTK_ADJUSTMENT (get_g_object (*gid));
 	gtk_range_set_adjustment (GTK_RANGE(widget), adjustment);
}

static void
set_adjustment_values (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	double *msg = data;
	int gid = (int) msg[ 0 ];
	GtkAdjustment *adjustment = NULL;
	adjustment = GTK_ADJUSTMENT(get_g_object (gid));
	if( adjustment == NULL) printf("adnull\n");
	//gtk_adjustment_configure (adjustment, msg[1], msg[2], msg[3], msg[4], msg[5], msg[6]);
}

static void
set_alignment (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	double *range = data;
	if (GTK_IS_FRAME(widget))
	{
		float xalign = (float) range[ 0 ];
		float yalign = (float) range[ 1 ];
		gtk_frame_set_label_align (GTK_FRAME(widget), xalign, yalign);
	}
	else if (GTK_IS_ALIGNMENT(widget))
	{
		gtk_alignment_set  (	GTK_ALIGNMENT(widget),
					(float) range[ 0 ],
					(float) range[ 1 ],
					(float) range[ 2 ],
					(float) range[ 3 ] );
	}
}

static void
set_angle (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gdouble *angle = data;
	gtk_label_set_angle (GTK_LABEL(widget), *angle);
}

static void
set_aspect_ratio (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GList *glist = data;
	float xalign = atof (read_str_from_glist(glist, 0));
	float yalign = atof (read_str_from_glist(glist, 1));
	float ratio = atof (read_str_from_glist(glist, 2));
	gboolean obey_child = str_to_boolean (read_str_from_glist(glist, 3));

	gtk_aspect_frame_set (GTK_ASPECT_FRAME(widget), xalign, yalign, ratio,  obey_child);
}

static void
set_border (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *w = data;
	gtk_container_set_border_width (GTK_CONTAINER(widget), *w);
}

static void
set_child (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
/*
gtk_button_box_set_child_secondary  (GtkButtonBox *widget,
                                                         GtkWidget *child,
                                                         gboolean is_secondary);
*/
}

static void
set_color (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
 	GdkColor color;
	int *c = data;
	color.red = c[ 0 ] << 8;
	color.green = c[ 1 ] << 8;
	color.blue = c[ 2 ] << 8;
	guint16 alpha = c[ 3 ] << 8;

	gtk_color_button_set_color (GTK_COLOR_BUTTON(widget), &color );
	gtk_color_button_set_alpha (GTK_COLOR_BUTTON(widget), alpha);
}

static void
set_current_folder (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), data);
}

static void
set_current_page (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *index = data;
	gtk_notebook_set_current_page ( GTK_NOTEBOOK(widget), *index);
}

static void
set_disabled (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gtk_toolbar_unset_style (GTK_TOOLBAR(widget));
}

static void
set_draw (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	gtk_scale_set_draw_value (GTK_SCALE(widget), *b );
}

static void
set_digits (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *i = data;
	if (GTK_IS_SPIN_BUTTON(widget))
	{
		guint ui = *i;
		gtk_spin_button_set_digits ( GTK_SPIN_BUTTON(widget), ui );
		gtk_widget_queue_draw ( widget );
	}
	else//GtkScale and subclasses
	{
		gtk_scale_set_digits (GTK_SCALE(widget), *i );
	}
}

static void
set_editable (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	gtk_text_view_set_editable (GTK_TEXT_VIEW(widget), *b);
}

static void
set_ellipsize (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	const char *idStr = data;
	PangoEllipsizeMode mode = get_pango_ellipsize_mode (idStr);
	if (GTK_IS_LABEL(widget)) gtk_label_set_ellipsize (GTK_LABEL(widget), mode);
	if (GTK_IS_PROGRESS_BAR(widget)) gtk_progress_bar_set_ellipsize(GTK_PROGRESS_BAR(widget), mode);
}

static void
set_expand (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	gtk_tool_item_set_expand (GTK_TOOL_ITEM(widget), *b);
}

static void
set_filename (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	const char *filename = data;
	gtk_file_chooser_set_filename (GTK_FILE_CHOOSER(widget), filename );
}

static void
set_font (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
 	gtk_font_button_set_font_name (GTK_FONT_BUTTON(widget), data);
}

static void
set_focus_on_click (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;

	/* GtkComboBox, GtkComboBoxEntry*/
	if (GTK_IS_COMBO_BOX(widget))
		gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX(widget), *b);

	/* GtkButton and subclasses */
	else 
		gtk_button_set_focus_on_click (GTK_BUTTON(widget), *b);
}

static void
set_from_stock (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GList *imgn = data;
	GList *sizen = imgn->next;
	const char *stockImg = imgn->data;
	const char *sizeStr = sizen->data;
	GtkIconSize size = get_icon_size( sizeStr );
	gtk_image_set_from_stock (GTK_IMAGE(widget), stockImg, size);
}

static void
set_homogenous (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	if (GTK_IS_TOOL_ITEM(widget))  gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM(widget), *b);
	else if (GTK_IS_BOX(widget)) gtk_box_set_homogeneous (GTK_BOX(widget), *b);
	else if (GTK_IS_TABLE(widget))
	{
		 gtk_table_set_homogeneous (GTK_TABLE(widget), *b);
	}
}

static void
set_image (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gtk_image_set_from_file (GTK_IMAGE(widget), data );
	gtk_widget_queue_draw (widget);
}

static void
set_increments (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	double *incr = data;
	gtk_range_set_increments (GTK_RANGE(widget), incr[ 0 ], incr[ 1 ]);
}

static void
set_intend (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *intend = data;
	gtk_text_view_set_indent (GTK_TEXT_VIEW(widget), *intend);
}

static void
set_items (GtkWidget *widget, gint data_type, gpointer pdata, gpointer data2 )
{
	GList *items = pdata;
	GList *li;
	gtk_list_store_clear (GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX(widget))));
	for(li = items; li!= NULL; li = g_list_next(li)) 
		gtk_combo_box_insert_text (GTK_COMBO_BOX (widget), 0, (char*) li->data);
}

static void
set_justification (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	//const gchar *jstr = data;
	GtkJustification just = get_gtk_justification( data );
	gtk_text_view_set_justification (GTK_TEXT_VIEW(widget), just);
}

static void
set_label (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	if (GTK_IS_LABEL(widget)) gtk_label_set_label (GTK_LABEL(widget), data);

	else if (GTK_IS_FRAME(widget))
	{
		gtk_frame_set_label (GTK_FRAME(widget), data);
		gtk_widget_queue_draw(widget);
	}

	else if (GTK_IS_TOOL_BUTTON(widget))
		gtk_tool_button_set_label (GTK_TOOL_BUTTON(widget), data);

	/* GtkButton and subclasses */
	else if (GTK_IS_BUTTON(widget)) gtk_button_set_label (GTK_BUTTON(widget), data);
}

static void
set_layout (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gtk_button_box_set_layout (GTK_BUTTON_BOX(widget), get_get_buttonbox_style (data));
}

static void
set_markup (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	 gtk_label_set_markup (GTK_LABEL(widget), data);
}

static void
set_max (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *max = data;
	gtk_entry_set_max_length (GTK_ENTRY(widget), *max);
}

static void
set_mode (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON(widget), *b);
}

static void 
set_numeric (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	gboolean *b = data;
	set_boolean_property (G_OBJECT(widget), "numeric", *b);
}

static void
set_orientation (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{

	if (GTK_IS_PROGRESS_BAR(widget))
	{
		int *ord = data;
		int orientation = get_gtk_progress_orientation ( *ord );
		gtk_progress_bar_set_orientation (GTK_PROGRESS_BAR(widget), orientation);
	}
	else//gtk tool bar
	{
		GtkOrientation orientation = get_gtk_widget_orientation (data);
		gtk_toolbar_set_orientation (GTK_TOOLBAR(widget), orientation);
	}
}

static void
set_padding (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *msg = data;
	gtk_alignment_set_padding (GTK_ALIGNMENT(widget), msg[0], msg[1], msg[2], msg[3]);
}

static void
set_position (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	if (GTK_IS_PANED(widget))
	{
		int *p_pos = data; 
		gtk_paned_set_position (GTK_PANED(widget), *p_pos );
	}
	else //GtkScale + desc
	{
		GtkPositionType pos = get_gtk_pos( data );
		gtk_scale_set_value_pos (GTK_SCALE(widget), pos);
	}
}

static void 
set_range (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	double *range = data;
	if (GTK_IS_SPIN_BUTTON(widget))
	{
		gtk_spin_button_set_range ( GTK_SPIN_BUTTON(widget), range[0], range[ 1 ] );
		gtk_widget_queue_draw ( widget );
	}
	else//GtkRange and subclasses
	{
		GtkAdjustment *ad = gtk_range_get_adjustment ( GTK_RANGE( widget ) );
		double psize = ad->page_size;
		gtk_range_set_range ( GTK_RANGE ( widget ), range[ 0 ], range[ 1 ] + psize );
		//gdouble val = gtk_range_get_value (GTK_RANGE ( widget ));from 2.12, current code for older version
		//gtk_range_set_fill_level (GTK_RANGE ( widget ), val );from 2.12, current code for older version
		gtk_widget_queue_draw ( widget );
	}
}

static void 
set_resizable (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	gboolean *b = data;
	if (GTK_IS_WINDOW(widget))
			gtk_window_set_resizable (GTK_WINDOW(widget), *b );

	else //GtkContainer + desc 
	{
		const gchar *mode_id = data;
		GtkResizeMode resize_mode = get_gtk_resize_mode( mode_id );
		gtk_container_set_resize_mode (GTK_CONTAINER(widget),resize_mode);
	}
}	

static void
set_sensitive (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	gboolean *b = data;
	set_boolean_property (G_OBJECT(widget), "sensitive", *b);
}

static void
set_shadow (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *msg = data;
	gtk_frame_set_shadow_type (GTK_FRAME(widget), *msg);
}

static void
set_show (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	gtk_toolbar_set_show_arrow (GTK_TOOLBAR(widget), *b);
}

static void
set_size (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	printf("ededede");
	if (GTK_IS_WINDOW(widget)) 
	{
		int *size = data;
		gtk_window_resize (GTK_WINDOW(widget), size[ 0 ], size[ 1 ]);
	}
	else if (GTK_IS_TOOLBAR(widget)) 
	{
		GtkIconSize isize = get_icon_size (data);
		gtk_toolbar_set_icon_size (GTK_TOOLBAR(widget), isize);
	}
}

static void
set_spacing (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *msg = data;
	if (GTK_IS_BOX(widget))
		gtk_box_set_spacing (GTK_BOX(widget), *msg);

	else // GtkTable
	{
		int op = msg[0];
		if (op == ROW_SPACING) gtk_table_set_row_spacing (GTK_TABLE(widget), (guint)msg[2], (guint)msg[1]);
		if (op == COLUMN_SPACING) gtk_table_set_col_spacing (GTK_TABLE(widget), (guint)msg[2], (guint)msg[1]);
		if (op == ALL_ROWS_SPACING) gtk_table_set_row_spacings (GTK_TABLE(widget), (guint)msg[1]);
		if (op == ALL_COLUMNS_SPACING) gtk_table_set_col_spacings (GTK_TABLE(widget), (guint)msg[1]);
	}
}

static void
set_step (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	double *step = data;
	gtk_progress_bar_set_pulse_step (GTK_PROGRESS_BAR(widget), *step);
}

static void
set_stock (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	if (GTK_IS_TOOL_BUTTON(widget)) gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON(widget), data);
	else if (GTK_IS_BUTTON(widget))
	{
		gboolean *b = data;
		gtk_button_set_use_stock (GTK_BUTTON(widget), *b);
	}
}

static void
set_style (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GtkToolbarStyle style = get_get_toolbar_style (data);
	gtk_toolbar_set_style (GTK_TOOLBAR(widget) ,style);
}

static void
set_text (GtkWidget *widget, gint data_type, gpointer data, gpointer data2  )
{
	const gchar *text = data;
	if (GTK_IS_ENTRY(widget)) gtk_entry_set_text ( GTK_ENTRY(widget), text );
	if (GTK_IS_TEXT_VIEW(widget))
	{
		GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(widget));
 		gtk_text_buffer_set_text (buffer, text, -1);
	}
	if (GTK_IS_PROGRESS_BAR(widget)) gtk_progress_bar_set_text (GTK_PROGRESS_BAR(widget), text );
}

static void 
set_title (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	if (GTK_IS_WINDOW(widget)) gtk_window_set_title (GTK_WINDOW( widget), data);
	if (GTK_IS_FILE_CHOOSER(widget)) gtk_file_chooser_button_set_title (GTK_FILE_CHOOSER_BUTTON(widget), data);
	if (GTK_IS_COLOR_BUTTON(widget)) gtk_color_button_set_title (GTK_COLOR_BUTTON(widget), data);
}

static void 
set_tooltip (GtkWidget *widget, gint data_type, gpointer data, gpointer data2 )
{
	//--- Note: this is deprecated stuff and should be replaced with some of the commented out code later
	const gchar *text = data;
	GtkTooltips *tips;
	tips = gtk_tooltips_new ();
   	gtk_tooltips_set_tip (GTK_TOOLTIPS (tips), GTK_WIDGET(widget), text, "");

  	//gtk_widget_set_tooltip_text ( GTK_WIDGET(widget) , text );
	//const gchar *text;// = gtk_widget_get_tooltip_text( widget );
	//text = gtk_widget_get_tooltip_text( GTK_WIDGET(widget ));
	//set_string_property ( widget, "tooltip-text", text );
}

static void
set_update_policy (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gtk_range_set_update_policy (GTK_RANGE( widget ), get_gtk_update_policy(data));
}

static void
set_use_size (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	gtk_font_button_set_use_size (GTK_FONT_BUTTON(widget), *b);
}

static void
set_use_underline (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	if (GTK_IS_BUTTON(widget)) gtk_button_set_use_underline (GTK_BUTTON(widget), *b );
	else gtk_tool_button_set_use_underline (GTK_TOOL_BUTTON(widget), *b);
}

static void 
set_value (GtkWidget *widget, gint data_type, gpointer data, gpointer data2  )
{
	gdouble *pvalue = data;
	if (GTK_IS_BUTTON(widget)) gtk_range_set_value (GTK_RANGE(widget), *pvalue );

	else if (GTK_IS_SPIN_BUTTON(widget))
	{
		GtkAdjustment *adjustment = gtk_spin_button_get_adjustment ( GTK_SPIN_BUTTON(widget));
		gtk_adjustment_set_value ( adjustment, *pvalue );
	}

	else if (GTK_IS_PROGRESS_BAR(widget)) gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(widget), *pvalue);
}

static void
set_width (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *width = data;
	if (GTK_IS_LABEL(widget)) gtk_label_set_width_chars (GTK_LABEL(widget), *width );
	if (GTK_IS_ENTRY(widget)) gtk_entry_set_width_chars (GTK_ENTRY(widget), *width );
}

static void
set_wrap (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	if (GTK_IS_SPIN_BUTTON(widget)) gtk_spin_button_set_wrap (GTK_SPIN_BUTTON(widget), *b);
	if (GTK_IS_LABEL(widget)) gtk_label_set_line_wrap(GTK_LABEL(widget), *b);
	if (GTK_IS_TEXT_VIEW(widget))
	{
		const gchar *modeS = data;
		GtkWrapMode mode = get_gtk_wrap( modeS );
 		gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW(widget), mode );
	}
}

static void
show (GtkWidget *widget, gint data_type, gpointer data, gpointer data2  )
{
	gtk_widget_show( widget );
}

static void
show_all (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gtk_widget_show_all( widget );
}

static void
show_size (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gboolean *b = data;
	gtk_font_button_set_show_size (GTK_FONT_BUTTON(widget), *b);
	gtk_widget_queue_draw (widget);
}

static void
size_request (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *size = data;
	gtk_widget_set_size_request (widget,size[ 0 ], size[ 1 ]);
}

static void
stack_op (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GList *sgl1 = data;
	GList *sgl2 = sgl1->next;
	GList *sgl3 = sgl2->next;

	const char *s1 = sgl1->data;
	int message_id = atoi(sgl2->data);
	int context_id = atoi(sgl3->data);
	
	if (strcmp( s1, "pop") == 0)
		gtk_statusbar_pop (GTK_STATUSBAR(widget), context_id);

	if (strcmp( s1, "remove") == 0)
		gtk_statusbar_remove (GTK_STATUSBAR(widget), context_id, message_id);
}

static void
stack_op_cr (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	if (GTK_IS_STATUSBAR(widget))
	{
		GList *tgl = data;
		GList *cgl = tgl->next;
		const char *text = tgl->data;
		int context_id = atoi(cgl->data);
		int mesid = gtk_statusbar_push ( GTK_STATUSBAR(widget), context_id, text);
		send_glade_signal ( widget, STACK_OP_CR, M_INT, (gpointer) &mesid );
	}
}

static void
tw_append (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *col_id = data;
	GtkTreeViewColumn *column;
	column = GTK_TREE_VIEW_COLUMN(get_g_object (*col_id));

	//set_boolean_property (G_OBJECT(column), "clickable", TRUE);
  	//g_object_set_property( column, "clickable", 
/*
	gtk_tree_view_set_cursor (GTK_TREE_VIEW(widget),
                                                         GtkTreePath *path,
                                                         GtkTreeViewColumn *focus_column,
                                                         gboolean start_editing);
*/
	//gtk_tree_view_column_set_sort_indicator (column, TRUE);
	gtk_tree_view_append_column (GTK_TREE_VIEW (widget), column);
}

static void
tw_col_set_sort_indicator (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GtkTreeViewColumn *column;
	GList *list = data;
	int col_id = atoi (read_str_from_glist (list, 1));
	column = GTK_TREE_VIEW_COLUMN (get_g_object (col_id));
	gboolean val = str_to_boolean (read_str_from_glist (list, 0));

	gtk_tree_view_column_set_sort_indicator (column, val);
}

static void
tw_get_selected (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(widget));
	GList *list = gtk_tree_selection_get_selected_rows (selection, NULL);
	if( list == NULL )
	{
		send_glade_signal ( widget, TW_GET_SELECTED, M_NULL, NULL );
		return;
	}

	int len = 0;
	GList *strs = NULL;
	while( list != NULL )
	{
		strs = g_list_append (strs, gtk_tree_path_to_string (list->data));
		list = list->next;
		len++;
	}

	send_glade_signal_two_msg ( widget, TW_GET_SELECTED, M_STR_ARR, strs, &len );
}

static void
tw_ls_append (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
/*

	int *gid = data;
	GtkListStore *list_store = NULL;
	GtkTreeIter iter;
	list_store = GTK_LIST_STORE(get_g_object (*gid));
	gtk_list_store_append (list_store, &iter);
*/
}

static void
tw_ls_set_value (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	printf("tw_ls_set_value called should be tree model in shit" );
/*
	int *int_data = data2;

	GType gtype = get_gtype (int_data[1]);
	const char *val_str = read_str_from_glist (data, 1);

	GtkListStore *list_store = GTK_LIST_STORE (get_g_object (int_data[2]));

	GtkTreeIter iter;
	gtk_tree_model_get_iter_first (GTK_TREE_MODEL(list_store), &iter);
	
	GValue gval = get_gvalue (gtype,val_str);
	gtk_list_store_set_value (list_store, &iter, 0, &gval);
*/
}

static void
tw_set_cursor (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GtkTreePath *path = gtk_tree_path_new_from_string (read_str_from_glist (data, 0));
	GtkTreeViewColumn *column = 
		GTK_TREE_VIEW_COLUMN(get_g_object (atoi (read_str_from_glist (data, 1))));
	gboolean ed =  str_to_boolean ( read_str_from_glist (data, 2));
	gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), path, column, ed);
}

static void
tw_set_model (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *g_id = data;
/*
	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(widget), TRUE);
	gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(widget), TRUE);
	gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(widget), GTK_TREE_VIEW_GRID_LINES_VERTICAL);
*/
	GtkTreeModel *tree_model = GTK_TREE_MODEL(get_g_object (*g_id));
	gtk_tree_view_set_model (GTK_TREE_VIEW(widget), tree_model);
}

static void
tw_set_sel_all (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(widget));
	gboolean *b = data;
	if (*b) gtk_tree_selection_select_all (selection);
	else gtk_tree_selection_unselect_all (selection);
}

static void
tw_set_sel_row (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	GtkTreePath *path = gtk_tree_path_new_from_string ( read_str_from_glist (data, 0));
	GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(widget));
	gboolean select = str_to_boolean ( read_str_from_glist (data, 1));
	if( select) gtk_tree_selection_select_path (selection, path);
	else gtk_tree_selection_unselect_path (selection, path);
}

static void
tw_set_selection (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	int *mode = data;
	GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(widget));
 	gtk_tree_selection_set_mode (selection, *mode);
}

static void//iter is dead
tw_tm_get_first (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
/*
	int *tree_id = data;
	GtkTreeModel *tree_model = GTK_TREE_MODEL(get_g_object (*tree_id));

	GtkTreeIter iter;
	gboolean not_empty = gtk_tree_model_get_iter_first (tree_model, &iter);
	GtkTreeIter *iter_p;	
	iter_p = g_new( GtkTreeIter, 1 );
	*iter_p = iter;
	int iter_id = save_iter (iter_p);
	if (!not_empty) iter_id = -1;
	send_glade_signal (widget, TW_TM_GET_FIRST, M_INT, (gpointer) &iter_id);
*/
}

static void
tw_tm_next (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
/*
	int *int_data = data;
	GtkTreeModel *tree_model = GTK_TREE_MODEL(get_g_object (int_data[0]));
	GtkTreeIter *iter = get_iter (int_data[1]);
	gtk_tree_model_iter_next (tree_model, iter);
*/
}

static void 
unmaximize (GtkWidget *widget, gint data_type, gpointer data, gpointer data2)
{
	gtk_window_unmaximize (GTK_WINDOW(widget));
}
